(function() {

	nsbase.views.message = new MessageFunc();

	function MessageFunc() {
		var _self = this;

		_self.init = init;

		var messageActions = nsbase.config.actionUrl.message,
			utilities = nsbase.utility,
			validations = nsbase.utility.validations,
        	clientResource = nsbase.resources.clientResource;

		var cssClass = {
			selected:"selected",
			error:"error"
		};

		var pageSize = -1;//取出所有的数据

		function init() {
      		$(".nav_menu li.Message").addClass(cssClass.selected);
			bindTimeShaft();
			bindMessageList();
			

		}


		// 绑定时间轴
		function bindTimeShaft () {
			$(".msg_date_nav ul>li").click(function(e){
				e.stopPropagation();
				var sender = $(this);
				sender.parent("ul").find("li").removeClass(cssClass.selected);
				sender.addClass(cssClass.selected);
				// 异步请求数据
				// var monthkey = sender.attr("date-time");
				// bindMessageList(monthkey);
			});

			$(".msg_date_nav ol>li").click(function(e){
				e.stopPropagation();
				var sender = $(this);
				sender.parent("ol").find("li").removeClass(cssClass.selected);
				sender.addClass(cssClass.selected);
				// 异步请求数据
				// var monthkey = sender.attr("date-time");
				// bindMessageList(monthkey);
			});
		}

		function bindMessageList (monthkey) {
			if(monthkey === undefined){
				monthkey = "all";
			}
			var $loadingDiv = $("#loading");
			$loadingDiv.show();
			var param = {
				monthkey: monthkey, 
		        page_index: 1, 
		        page_size : pageSize
			}
			nsbase.ajax.request(messageActions.getMessageList, param, function(result) {
				$loadingDiv.hide();
				if(result.success){
					bindData(result.data);
				} 
			},function(){
				$loadingDiv.hide();
			});
		}

		function bindData (messageData) {
			if(!messageData|| messageData.message_cnt == 0){
				return;
			}

			var $tplContainer = $("#tmpMsgContainer");
			$.each(messageData.messages,function(index,msgItem){
				var monthStr = utilities.dateFormat(msgItem.message_time,"yyyyMM");
				var $msgContainer = $("a[name='"+ monthStr +"']").next("ul");
				var $itemObj = $tplContainer.clone();
				if(index%2 == 0){
					$itemObj.find(".msg_box").addClass("left");
				} else{
					$itemObj.find(".msg_box").addClass("right");
				}
				// 站内消息，添加button 
				if(msgItem.type == nsbase.enums.messageType.inStation){
					var messageStatus = nsbase.enums.messageStatus.getEnumByValue(msgItem.status);
					$itemObj.find(".content").append(getMsgBottom(messageStatus));
				}
				// 绑定html
				$msgContainer.append(utilities.tmplFormat($itemObj.html(),msgItem,msgFormater));

				$msgContainer.find(".btnIgnore").click(function () {
					dealMessage($(this),nsbase.enums.messageStatus.ignore);
				});
				$msgContainer.find(".btnAgree").click(function(){
					dealMessage($(this),nsbase.enums.messageStatus.agree);
				});
			});

			//是否显示更多按钮
			// var pageCount = messageData.message_cnt / pageSize;
			// if(messageData.message_cnt % pageSize > 0){
			// 	pageCount = pageCount + 1;
			// }
		}

		function msgFormater (msgItem,dataIndex) {
			if(dataIndex == "message_time"){
				return utilities.dateFormat(msgItem.message_time,"yyyy-MM-dd");
			}
			if(dataIndex == "type"){
				var messageType = nsbase.enums.messageType.getEnumByValue(msgItem.type);
				return messageType.text;
			}
			if(dataIndex == "msg_type"){
				var messageType = nsbase.enums.messageType.getEnumByValue(msgItem.type);
				return messageType.key;
			}
		}

		// get invite html button code
		function getMsgBottom(messageStatus){
			var $tplBottom = $('<div class="bottom">\
			                      <a class="button btnIgnore" href="###">'+ clientResource.linkIgnore +'</a>\
			                      <a class="button btnAgree" href="###">'+ clientResource.linkAgree +'</a>\
			                    </div>');
			if(messageStatus == nsbase.enums.messageStatus.ignore ||
				messageStatus == nsbase.enums.messageStatus.agree){
				$tplBottom.html('<span class="result">'+ messageStatus.text +'</span>');
			}

			
			return  $tplBottom;
		}

		function dealMessage(sender,messageStatus){
			var $messageBox = sender.parents(".msg_box:first");
			$messageBox.mask();
			var param = {
				message_id:$messageBox.attr("attr-id"),
				message_status:messageStatus.value
			};

			nsbase.ajax.request(messageActions.dealMessage, param, function(result) {
				$messageBox.unmask();
				if(result.success){
					$messageBox.find(".bottom").html('<span class="result">'+messageStatus.text+'</span>')
				}
			},function(){
				$messageBox.unmask();
			});
		}







		// 鼠标 滚动事件
		// ScrollPage(window, { delay: 1000, marginBottom: 0 }, function (offset) {
	  //           if (offset > 0) {
	  //               $("#Loadding").show(); //加载提示
	  //               setTimeout(function () {
	  //                   //这里就是异步获取内容的地方，这里简化成一句话，可以根据需要修改
	  //                   $(".list_main").append($("<div class='content' style='height:100px;'>第“" + ($(".content").size() + 1) + "”页内容</div>"));
	  //                   //内容获取后，隐藏加载提示
	  //                   $("#Loadding").hide();
	  //               }, 1000);
	  //           }
	  //       });
		function ScrollPage(obj, options, callback) {
	        var _defaultOptions = { delay: 500, marginBottom: 100 }; //默认配置：延迟时间delay和滚动条距离底部距离marginBottom
	        options = $.extend(_defaultOptions, options);
	        this.isScrolling = false; //是否在滚动
	        this.oriPos = 0; //原始位置
	        this.curPos = 0; //当前位置
	        var me = this; //顶层
	        var $obj = (typeof obj == "string") ? $("#" + obj) : $(obj);
	        //绑定滚动事件
	        $obj.scroll(function (ev) {
	            me.curPos = $obj.scrollTop();
	            if ($(window).height() + $(window).scrollTop() >= $(document.body).height() - options.marginBottom) {
	                if (me.isScrolling == true) return;
	                me.isScrolling = true;
	                setTimeout(function () { me.isScrolling = false; }, options.delay);   //重复触发间隔毫秒
	                if (typeof callback == "function") callback.call(null, me.curPos - me.oriPos);
	            };
	            me.oriPos = me.curPos;
	        });
	    };
	}

	

})();